package model;

import controller.Controller;

/**
 * @author Colin
 * 
 * The Ammo class is for the bullets that leave the weapons.
 * Ammo has an associated velocity which is the speed and direction
 * in which it is traveling. For automatic weapons Ammo will fire at 
 * the zombosaur that is the closest on the trail to the spaceship.
 *
 */
public class Ammo {
	
	private Velocity velocity;
	private float x; //Current x position
	private float y; //Current y position
	private int damage;
	private Weapon weapon; 	//the weapon shooting this ammo
	
	//Empty Ammo 
	public Ammo(Weapon weapon){
		this.weapon = weapon;
	}
	
	//Ammo with velocity and x, y, positions
	public Ammo( Velocity velocity, float x, float y, Weapon weapon){
		this.velocity = velocity;
		this.x = x;
		this.y = y;
		this.weapon = weapon;
	}
	
	/**
	 * Updates position of the ammo on the map
	 */
	public void update(){
		//if the ammo is close to its final position, set it to the final position
		//otherwise, update its position
		if( x > velocity.getX2() - 0.15 && x < velocity.getX2() + 0.15 && y > velocity.getY2() - 0.15 && y < velocity.getY2() + 0.15 ){
			x = velocity.getX2();
			y = velocity.getY2();
		}
		else{
			//These statements determine if the ammo should go up or down and left or right
			if(velocity.getDy() < 0){
				y -= Math.abs((velocity.getSpeed()/Controller.ticksPerSecond) * Math.sin(velocity.getAngle()));
			}
			else{
				y += Math.abs((velocity.getSpeed()/Controller.ticksPerSecond) * Math.sin(velocity.getAngle()));
			}
			if(velocity.getDx() < 0){
				x -= Math.abs((velocity.getSpeed()/Controller.ticksPerSecond) * Math.cos(velocity.getAngle()));
			}
			else{
				x += Math.abs((velocity.getSpeed()/Controller.ticksPerSecond) * Math.cos(velocity.getAngle()));
			}
		}
	}
	
	public Velocity getVelocity(){
		return velocity;
	}
	public void setVelocity( Velocity velocity ){
		this.velocity = velocity;
	}
	
	public float getX(){
		return x;
	}
	public void setX( int x ){
		this.x = x;
	}
	
	public float getY(){
		return y;
	}
	public void setY( int y ){
		this.y = y;
	}
	
	/**
	 * returns the x coord of the center of the ammo
	 * @return
	 */
	public float getCenterX(){
		return x + 0.5f;
	}
	
	/**
	 * returns the y coord of the center of the ammo
	 * @return
	 */
	public float getCenterY(){
		return y + 0.5f;
	}

	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

	public Weapon getWeapon() {
		return weapon;
	}

	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
	}

}
